package com.zzgc.common.config;

import com.zzgc.common.fliter.CrosFilter;
import com.zzgc.common.fliter.JWTTokenFilter;
import com.zzgc.common.fliter.RootRoleFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SecurityConfig {

    /**
     * 添加JWTTokenFilter
     * @param jwtTokenFilter
     * @return
     */
    @Bean
    public FilterRegistrationBean jwtTokenFilterRegistration(@Autowired JWTTokenFilter jwtTokenFilter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(jwtTokenFilter);
        registration.addUrlPatterns("/*");
        registration.setName("jwtTokenFilter");
        registration.setOrder(2);
        return registration;
    }

    /**
     * 跨域请求过滤器
     * @param crosFilter
     * @return
     */
    @Bean
    public FilterRegistrationBean crosFilterRegistration(@Autowired CrosFilter crosFilter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(crosFilter);
        registration.addUrlPatterns("/*");
        registration.setName("crosFilter");
        registration.setOrder(1);
        return registration;
    }

    /**
     * 添加ROOT管理员角色过滤器
     * @param rootRoleFilter
     * @return
     */
    @Bean
    public FilterRegistrationBean RootRoleFilterRegistration(@Autowired RootRoleFilter rootRoleFilter){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(rootRoleFilter);
        // 设置需要县教育局角色的请求路径
        registration.addUrlPatterns("/auth/user/register");
//        registration.addUrlPatterns("/test/test");
        registration.setName("RootRoleFilter");
        registration.setOrder(2);
        return registration;
    }

}
